package cn.edu.nuc.zyb.client;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

import cn.edu.nuc.zyb.model.Cart;
import cn.edu.nuc.zyb.model.Goods;

/**
 * 这个类是我的客户端类
 * 
 * @author 张彦博
 *
 */

public class UserClient {

	Socket socket;

	/**
	 * UserClient()构造方法 负责与服务器进行连接
	 */
	public UserClient() {
		try {
			socket = new Socket("127.0.0.1", 4000);
		} catch (Exception e) {
			System.out.println("连接失败！");
		}
	}

	/**
	 * sendMsg()方法负责向服务器发送login消息与接收从服务器返回来的消息
	 * 
	 * @param username用户名
	 * @param password密码
	 * @return boolean值
	 */
	public boolean sendMsg(String username, int password) {
		try {

			// 发送数据
			DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
			String msg = "login," + username + "," + password;
			System.out.println(msg);
			dos.writeUTF(msg);

		} catch (Exception e) {
			e.printStackTrace();
		}
		try {
			InputStream is = socket.getInputStream();
			DataInputStream dis = new DataInputStream(is);
			return dis.readBoolean();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return false;

	}

	/**
	 * acceptMsg()方法负责接收服务器返回来的boolean值
	 * 
	 * @return boolean值
	 */
	public boolean acceptMsg() {
		// InputStream is;
		// DataInputStream dis;
		try {
			InputStream is = socket.getInputStream();
			DataInputStream dis = new DataInputStream(is);
			return dis.readBoolean();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return false;
	}

	/**
	 * listGoods()方法负责向服务器发送展示商品列表的消息
	 */
	public void listGoods() {
		String msg = "listgoods,";
		OutputStream os = null;
		DataOutputStream dos = null;
		try {
			os = socket.getOutputStream();
			dos = new DataOutputStream(os);
			dos.writeUTF(msg);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * acceptListGoods()方法负责接收服务器返回的商品列表
	 * 
	 * @return 商品列表的list集合
	 */
	public List<Goods> acceptListGoods() {
		try {
			ObjectInputStream oos = new ObjectInputStream(socket.getInputStream());
			@SuppressWarnings("unchecked")
			List<Goods> list = (List<Goods>) oos.readObject();
			System.out.println(list.size());
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * searchGoods()方法负责向服务器发送查询的消息
	 * 
	 * @param goodsName商品名
	 * 
	 */
	public void searchGoods(String goodsName) {
		String msg = "searchgoods," + goodsName;
		OutputStream os = null;
		DataOutputStream dos = null;
		try {
			os = socket.getOutputStream();
			dos = new DataOutputStream(os);
			dos.writeUTF(msg);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * acceptSearchGoods()方法负责接收从服务器返回来的数据
	 * 
	 * @return Goods 返回一个Goods对象
	 */
	public Goods acceptSearchGoods() {
		try {
			ObjectInputStream oos = new ObjectInputStream(socket.getInputStream());
			@SuppressWarnings("unchecked")
			List<Goods> list = (ArrayList<Goods>) oos.readObject();
			System.out.println(list.size());
			return list.get(0);
		} catch (Exception e) {
			System.out.println("没有该货物！");
		}
		return null;
	}

	/**
	 * addUser()方法负责向服务器发送注册的消息
	 *
	 * @param username用户名
	 * @param password密码
	 * @param name姓名
	 * @param age年龄
	 * 
	 *
	 **/
	public void addUser(String username, int password, String name, int age) {
		String msg = "regist," + username + "," + password + "," + name + "," + age;
		try {
			OutputStream os = socket.getOutputStream();
			DataOutputStream dos = new DataOutputStream(os);
			dos.writeUTF(msg);
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	/**
	 * lookGoods()方法负责向服务器发送查看货物详细信息的消息
	 * 
	 * @param goodname商品名称
	 * 
	 */
	public void lookGoods(String goodname) {
		String msg = "lookgoods," + goodname;
		try {
			OutputStream os = socket.getOutputStream();
			DataOutputStream dos = new DataOutputStream(os);
			dos.writeUTF(msg);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * acceptLookGoods()接收从服务器返回的消息
	 * 
	 * @return Goods对象
	 */
	public Goods acceptLookGoods() {
		// 接收到服务器返回来的消息
		try {
			ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
			return (Goods) ois.readObject();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * addCart方法负责发送加入购物车的消息
	 * 
	 * @param id商品id
	 * @param name商品名称
	 * @param price商品价格
	 * @param num商品数量
	 * @param un用户名
	 */
	public void addCart(int id, String name, float price, int num, String un) {
		String msg = "addcart," + id + "," + name + "," + price + "," + num + "," + un;
		try {
			OutputStream os = socket.getOutputStream();
			DataOutputStream dos = new DataOutputStream(os);
			dos.writeUTF(msg);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 此方法展示购物车
	 * 
	 * @param name用户名
	 */
	public void listCart(String name) {
		String msg = "listcart," + name;
		try {
			OutputStream os = socket.getOutputStream();
			DataOutputStream dos = new DataOutputStream(os);
			dos.writeUTF(msg);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 此方法接收从服务器发送会来的购车列表
	 * 
	 * @return List<Cart>列表
	 */
	@SuppressWarnings("unchecked")
	public List<Cart> acceptAddCart() {
		try {
			ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
			return (List<Cart>) ois.readObject();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 发送结账消息
	 * 
	 * @param name用户名
	 * @param money总金额
	 */
	public void settleAccounts(String name, float money) {
		String msg = "settleaccounts," + name + "," + money;
		try {
			OutputStream os = socket.getOutputStream();
			DataOutputStream dos = new DataOutputStream(os);
			dos.writeUTF(msg);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 告诉服务器登陆的消息
	 * 
	 * @param name用户名
	 */
	public void exitLogin(String name) {
		String msg = "exitlogin," + name;
		try {
			OutputStream os = socket.getOutputStream();
			DataOutputStream dos = new DataOutputStream(os);
			dos.writeUTF(msg);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
